package com.edu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:何包蛋
 * @Date :2022/11/12 - 11 - 12 - 11:47
 */
@Component
@Aspect
public class LogAdvise {

    @Pointcut("execution(* com.edu.service.BookService..get*(..))")
    public void pointcut(){

    }

    @Before("pointcut()")
    public void before(JoinPoint jt){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(jt.getSignature().getName()+"执行于："+df.format(new Date()));
    }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint jt) throws Throwable {
        long start = System.currentTimeMillis();
        Object o = jt.proceed();
        long end = System.currentTimeMillis();
        System.out.println(jt.getSignature().getName()+"执行耗时："+(end-start));
        return o;
    }

}
